package com.xindaoapp.happypet.fragments;

import java.util.List;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindao.photonics.fragments.manager.BaseFragment;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.bean.GoodsRecord;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.utils.IRequest;

public class NiuniuOrderFragment extends BaseFragment {

    private RelativeLayout mNoData, mLeft;
    private PullToRefreshListView mPullToRefreshListView;

    private TextView vTitle, vLeft;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_niuniuorder, container, false);
    }

    @Override
    protected void initViews(View view) {
        super.initViews(view);
        mNoData = (RelativeLayout) view.findViewById(R.id.no_data);
        mPullToRefreshListView = (PullToRefreshListView) view.findViewById(R.id.listview);
        mPullToRefreshListView.setMode(Mode.PULL_FROM_START);
        mPullToRefreshListView.setOnRefreshListener(mOnRefreshListener);

        vTitle = (TextView) view.findViewById(R.id.title);
        vTitle.setText("牛牛币订单");
        mLeft = (RelativeLayout) view.findViewById(R.id.left);
    }

    @Override
    protected void initEvents(View view) {
        super.initEvents(view);
        mLeft.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                mActivity.finish();
            }
        });

    }

    private final OnRefreshListener<ListView> mOnRefreshListener =
            new OnRefreshListener<ListView>() {

                @Override
                public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                    onLoadData();
                }
            };


    @Override
    protected void onLoadData() {
        super.onLoadData();
        if (null != getArguments()) {
            String exid = getArguments().getString("ex_id");
        }

        getMoccaApi().getGoodsRecord(Constants.PAGE_FIRST, Constants.PAGE_SIZE,
                new IRequest<List<GoodsRecord>>() {
                    @Override
                    public void request(List<GoodsRecord> t) {
                        if (t != null) {
                            // mGoodsRecord = t;
                            mNoData.setVisibility(View.GONE);

                            mPullToRefreshListView.setAdapter(new MyAdapter(t));
                            onDataArrived(true);
                        } else {
                            mNoData.setVisibility(View.VISIBLE);
                            onDataArrived(false);
                        }
                        mPullToRefreshListView.onRefreshComplete();
                    }
                });
    }


    @Override
    protected String getTitleName() {
        return "牛牛币订单";
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }


    class MyAdapter extends BaseAdapter {

        List<GoodsRecord> list = null;

        public MyAdapter(List<GoodsRecord> goods) {
            super();
            this.list = goods;
        }

        @Override
        public int getCount() {

            return list.size();
        }

        @Override
        public Object getItem(int position) {

            return null;
        }

        @Override
        public long getItemId(int position) {

            return 0;
        }

        @Override
        public View getView(int position, View contentView, ViewGroup arg2) {

            ViewHolder holder;

            if (contentView == null) {
                contentView = View.inflate(mActivity, R.layout.item_myrecord_duihuan, null);
                holder = new ViewHolder();
                // holder.tv_line = (TextView) contentView.findViewById(R.id.tv_line);
                holder.icon = (ImageView) contentView.findViewById(R.id.icon);
                holder.tv_name = (TextView) contentView.findViewById(R.id.tv_name);
                holder.tv_jifen = (TextView) contentView.findViewById(R.id.tv_jifen);
                holder.tv_time = (TextView) contentView.findViewById(R.id.tv_time);
                holder.tv_status = (TextView) contentView.findViewById(R.id.tv_status);
                contentView.setTag(holder);
            } else {
                holder = (ViewHolder) contentView.getTag();
                // holder.iv_good.setImageResource(R.drawable.actionbar_back_indicator);
            }

            // if (position == 0) {
            // holder.tv_line.setVisibility(View.VISIBLE);
            // }

            GoodsRecord goods = list.get(position);
            ImageLoader.getInstance().displayImage(goods.goods_thumb, holder.icon);
            holder.tv_name.setText(goods.goods_name);
            holder.tv_jifen.setText(goods.exchange_integral);
            holder.tv_time.setText(goods.updatetime);

            return contentView;
        }

        class ViewHolder {
            ImageView icon;
            TextView tv_jifen, tv_status, tv_name, tv_time, tv_line;

        }

    }


    @Override
    protected boolean showTopTabBar() {
        return false;
    }



}
